﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GaussSolution;
using GaussianMethod;
using MathWorks.MATLAB.NET.Arrays;

namespace MatlabGaussSolution
{
    public class MatlabGaussSolution : IGaussSolution
    {
        private GaussianMethod.GaussianMethod method = new GaussianMethod.GaussianMethod();

        private MWNumericArray ToMWArray(double[] matrix)
        {
            return new MWNumericArray(matrix.Length, 1, matrix);
        }

        private MWNumericArray ToMWArray(double[,] matrix)
        {
            List<double> array = new List<double>(matrix.GetLength(0) * matrix.GetLength(1));
            foreach (double val in matrix)
                array.Add(val);
            return new MWNumericArray(matrix.GetLength(0), matrix.GetLength(1), array.ToArray());
        }

        private double[] ToDefaultArray(MWNumericArray array)
        {
            return (double[])array.ToVector(MWArrayComponent.Real); 
        }

        public double[] Solve(double[,] aMatrix, double[] bMatrix)
        {
            return ToDefaultArray((MWNumericArray)method.SolveEquationSystemGauss(ToMWArray(aMatrix), ToMWArray(bMatrix)));
        }
    }
}
